package ui.gui;

import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.*;

import domein.DomeinController;

public class VijandenPanel extends JPanel implements ActionListener {

	private ButtonGroup btnGroup;
	private JToggleButton[] cmd;
	private String[][] vijanden;
	
	public VijandenPanel(ButtonGroup btnGroup) {
		this.btnGroup = btnGroup;
		
		this.setLayout(new FlowLayout());
		
		this.vijanden = DomeinController.getInstance().geefPersonageCategorieen();
		
		this.initComponents();
	}
	
	private void initComponents() {
		cmd = new JToggleButton[vijanden.length];
		
		for(int i=0; i<vijanden.length; i++) {
			Icon icon = new ImageIcon("assets/textures/SoilSand0056_1_S.jpg");
			
			/*if(vijanden[i][1].equals("*")) {
				icon = createIcon("assets/textures/tree.png");
			}
			else if(vijanden[i][1].equals("+")) {
				icon = createIcon("assets/textures/BrickSmallReinforced0013_2_S.jpg");
			}
			else if(vijanden[i][1].equals("O")) {
				icon = createIcon("assets/textures/WaterPlain0008_2_S.jpg");
			}*/
			
			cmd[i] = new JToggleButton(vijanden[i][0]);
			cmd[i].addActionListener(this);
			cmd[i].setToolTipText(vijanden[i][0]);
			
			btnGroup.add(cmd[i]);
			
			add(cmd[i]);
		}
	}
	
	private ImageIcon createIcon(String path) {	
		BufferedImage drawImage = new BufferedImage(64, 64, BufferedImage.TYPE_INT_ARGB);
		
		try {
			BufferedImage bImage = ImageIO.read(new File(path));
			
			Graphics g = drawImage.getGraphics();
			g.drawImage(new ImageIcon(bImage).getImage(), 0, 0, 64, 64, null);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return new ImageIcon(drawImage);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		for(int i=0; i<vijanden.length; i++) {
			if(e.getSource() == cmd[i]) {
				DomeinController.getInstance().selecteerVijand(vijanden[i][0]);				
			}
		}
	}
}